﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Janus.Windows.UI;
using Janus.Windows.UI.CommandBars;
using HiStaff.Control;
using HiStaff.Util;
using HiStaff.iSystem.Properties;

namespace HiStaff.Base
{
    public static class CommandBarManager
    {
        public enum ActionCommand
        {
            Null = -1,
            Separator = 0,
            New = 1,
            Edit = 2,
            Save = 3,
            Cancel = 4,
            Delete = 5,
            Exit = 6,
            Validate = 7,
            Calculate = 8,
            Lock = 9,
            UnLock = 10,
            SLock = 11,
            ExportExcel = 12,
            ImportExcel = 13,
            Find = 14,
            ImportAnotherFile = 15,
            SelectObject = 16,
            Approve = 17,
            NotApprove = 18,
            ImportFromSwipeMC = 19,
            GenShift = 20,
            ApproveFlexible = 21,
            ImportSignManually = 22,
            ExportTemplate = 23,
            Print = 24,
        }
        public static string ListActionView = ",New,Edit,Save,Cancel,Delete,Validate,Calculate,Lock,UnLock,SLock,ImportExcel,ImportAnotherFile,SelectObject,Approve,NotApprove,ImportFromSwipeMC,GenShift,ImportSignManually,ExportTemplate,Print,";
        //,Exit,Find,ExportExcel
        private static string[] CommandText = { "", "Thêm", "Sửa", "Lưu", "Hủy", "Xóa", "Thoát", "Kiểm tra", 
                                                "Tổng hợp", "Khoá", "Mở khoá", "Khoá vĩnh viễn", "Xuất Excel", 
                                                "Tải dữ liệu từ Excel", "Tìm kiếm", "Tải dữ liệu từ tệp khác", 
                                                "Chọn", "Duyệt", "Không duyệt", "Tải dữ liệu từ máy chấm công", "Tạo ca tự động","Duyệt linh động", "Import dữ liệu chấm công tay","Xuất file mẫu import","In"};
        private static string[] CommandToolTip = { "", "Thêm (CtrlN)", "Sửa (F2)", "Lưu (CtrlS)", "Hủy (CtrlZ)", "Xóa (CtrlD)", "Thoát (CtrlQ)", "Kiểm tra (CtrlK)", 
                                                   "Tổng hợp (CtrlT)", "Khoá", "Mở khoá", "Khoá vĩnh viễn", "Xuất Excel", 
                                                   "Tải dữ liệu từ Excel", "Tìm kiếm(CtrlF)", "Tải dữ liệu từ tệp khác", 
                                                   "Chọn", "Duyệt", "Không duyệt", "Tải dữ liệu từ máy chấm công", "Tạo ca tự động","Duyệt linh động", "Import dữ liệu chấm công tay","Xuất file mẫu import","In (CtrlP)" };
        private static Shortcut[] CommandShotcut = { Shortcut.None, Shortcut.CtrlN, Shortcut.F2, Shortcut.CtrlS, Shortcut.CtrlZ, Shortcut.CtrlD, Shortcut.CtrlQ, 
                                                     Shortcut.CtrlK, Shortcut.CtrlT, Shortcut.None, Shortcut.None, Shortcut.None, Shortcut.None, 
                                                     Shortcut.None, Shortcut.CtrlF, Shortcut.None, Shortcut.None, 
                                                     Shortcut.None, Shortcut.None, Shortcut.None, Shortcut.None,Shortcut.None,Shortcut.None,Shortcut.None, Shortcut.CtrlP };

        public static void CreateCommandBar(HiCommandManager UICManager,
                                            UICommandBar UICBar,
                                            params ActionCommand[] commands)
        {
            UICManager.VisualStyle = VisualStyle.Office2010;
            UICommand[] UICList = new UICommand[commands.Length];
            UICommand UICObject;

            for (int idx = 0; idx < commands.Length; idx++)
            {
                var cmd = commands[idx];

                if (cmd == ActionCommand.Null)
                {
                    continue;
                }

                if (cmd == ActionCommand.Separator)
                {
                    UICObject = new UICommand("Separator");
                    UICObject.CommandType = CommandType.Separator;
                    UICList[idx] = UICObject;
                    continue;
                }

                UICObject = new UICommand(ConvertHelper.ConvertEnumToString<ActionCommand>(cmd), CommandText[Convert.ToInt32(cmd)]);
                UICObject.Image = GetImageByCommand(cmd);
                UICObject.Name = ConvertHelper.ConvertEnumToString<ActionCommand>(cmd);
                UICObject.Tag = ConvertHelper.ConvertEnumToString<ActionCommand>(cmd);
                UICObject.Shortcut = CommandShotcut[Convert.ToInt32(cmd)];
                UICObject.ToolTipText = CommandToolTip[Convert.ToInt32(cmd)];
                UICList[idx] = UICObject;
            }

            UICBar.Commands.AddRange(UICList);
        }

        public static void CreateContextMenuItem(UICommandManager UICManager,
                                                 UIContextMenu UICtxMenu,
                                                 bool hasSeparator, string parentkey, string itemkey, string itemtext)
        {
            UICManager.VisualStyle = VisualStyle.Office2010;
            UICommand UICObject;

            if (hasSeparator)
            {
                UICObject = new UICommand("Separator");
                UICObject.CommandType = CommandType.Separator;
                UICtxMenu.Commands.Add(UICObject);
            }

            UICObject = new UICommand(itemkey, itemtext);
            //UICObject.Image = GetImageByCommand(cmd);
            UICObject.Name = itemkey;
            UICObject.Tag = itemkey;
            //UICObject.Shortcut = CommandShotcut[Convert.ToInt32(cmd)];
            UICObject.ToolTipText = itemtext;

            if (string.IsNullOrEmpty(parentkey))
                UICtxMenu.Commands.Add(UICObject);
            else
            {
                if (UICManager.Commands.Contains(parentkey))
                    UICManager.Commands[parentkey].Commands.Add(UICObject);
            }
        }

        public static void CreateTxtInCommandBar(HiCommandManager UICManager, UICommandBar UICBar,
                                                     string CmdKey, string title, int width)
        {
            UICManager.VisualStyle = VisualStyle.Office2010;
            UICommand[] UICList = new UICommand[3];
            UICommand UICObject;

            //if (cmd == ActionCommand.Separator)
            //{
            //    UICObject = new UICommand("Separator");
            //    UICObject.CommandType = CommandType.Separator;
            //    UICList[idx] = UICObject;
            //    continue;
            //}

            UICObject = new UICommand("Label" + CmdKey);
            UICObject.CommandType = CommandType.Label;
            UICObject.Text = title;
            UICList[0] = UICObject;

            UICObject = new UICommand(CmdKey);
            
            UICObject.CommandType = CommandType.TextBoxCommand;
            UICObject.ControlWidth = width;
            UICList[1] = UICObject;

            UICObject = new UICommand("Separator");
            UICObject.CommandType = CommandType.Separator;
            UICList[2] = UICObject;

            UICBar.Commands.AddRange(UICList);
        }



        public static void CreateDropdownInCommandBar(HiCommandManager UICManager, UICommandBar UICBar,
                                                    string CmdKey, string title, int width, int dropdownitem)
        {
            UICManager.VisualStyle = VisualStyle.Office2010;
            UICommand[] UICList = new UICommand[3];
            UICommand UICObject;

            //if (cmd == ActionCommand.Separator)
            //{
            //    UICObject = new UICommand("Separator");
            //    UICObject.CommandType = CommandType.Separator;
            //    UICList[idx] = UICObject;
            //    continue;
            //}

            UICObject = new UICommand("Label" + CmdKey);
            UICObject.CommandType = CommandType.Label;
            UICObject.Text = title;
            UICList[0] = UICObject;

            UICObject = new UICommand(CmdKey);
            UICObject.CommandType = CommandType.ComboBoxCommand;
            UICObject.ControlWidth = width;
            UICList[1] = UICObject;

            UICObject = new UICommand("Separator");
            UICObject.CommandType = CommandType.Separator;
            UICList[2] = UICObject;

            UICBar.Commands.AddRange(UICList);
            UICBar.Commands[CmdKey].GetUIComboBox().MaxDropDownItems = dropdownitem;
        }

        private static Bitmap GetImageByCommand(ActionCommand command)
        {
            Bitmap img = null;

            switch (command)
            {
                //Black
                case ActionCommand.New:
                    img = Resources.add;
                    break;
                case ActionCommand.Edit:
                    img = Resources.edit;
                    break;
                case ActionCommand.Save:
                    img = Resources.save;
                    break;
                case ActionCommand.Cancel:
                    img = Resources.canceltb;
                    break;
                case ActionCommand.Delete:
                    img = Resources.delete;
                    break;
                case ActionCommand.Exit:
                    img = Resources.exit;
                    break;
                case ActionCommand.Validate:
                case ActionCommand.Approve:
                    img = Resources.checkmark;
                    break;
                case ActionCommand.ApproveFlexible:
                    //img = Properties.Resources.Validate2;
                    break;
                case ActionCommand.Calculate:
                    //img = Properties.Resources.Calculator;
                    break;
                case ActionCommand.Lock:
                    //img = Properties.Resources.Lock;
                    break;
                case ActionCommand.UnLock:
                    //img = Properties.Resources.Unlock;
                    break;
                case ActionCommand.SLock:
                    //img = Properties.Resources.SLock;
                    break;
                case ActionCommand.ExportExcel:
                    //img = Properties.Resources.ExportExcel;
                    break;
                case ActionCommand.ImportExcel:
                    //img = Properties.Resources.ExportExcel;
                    break;
                case ActionCommand.Find:
                    //img = Properties.Resources.Find;
                    break;
                case ActionCommand.ImportAnotherFile:
                    //img = Properties.Resources.Import;
                    break;
                case ActionCommand.NotApprove:
                    //img = Properties.Resources.Reject;
                    break;
                case ActionCommand.ImportFromSwipeMC:
                    //img = Properties.Resources.Import;
                    break;
                case ActionCommand.SelectObject:
                    //img = Properties.Resources.Validate;
                    break;
                case ActionCommand.GenShift:
                    //img = Properties.Resources.Import;
                    break;
                case ActionCommand.ImportSignManually:
                    //img = Properties.Resources.ExportExcel;
                    break;
                case ActionCommand.ExportTemplate:
                    //img = Properties.Resources.ExportExcel;
                    break;
                case ActionCommand.Print:
                    break;
            }
            return img;
        }
    }
}
